<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class ShopController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Shop");

	}
	
	function index(){



		if(I("get.keyword")){

			$map['name']=array("like","%".trim(I("get.keyword"))."%");
			$map['tel']=array("like","%".trim(I("get.keyword"))."%");
			$map['lianxiren']=array("like","%".trim(I("get.keyword"))."%");
			$search=true;

		}

		$this->assign("search",$search);

		$list=$this->db->where($map)->select();

		foreach($list as $k=>$v){

			$list[$k][uid]=D("Users")->where(array("shop_id"=>$v["id"]))->getField("id");
		}



		

		$this->assign("list",$list);
		
		$this->display();
	}
	

	
	
	function add(){

		if(IS_POST){

			if ($this->db->create($_POST)) {
				if ($this->db->add()!==false) {

//					echo "添加成功";

					$this->success("添加成功！", U("index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{



			$this->display("form");
		}
		}

	
	function edit(){
		
		
		if(IS_POST){
			if ($this->db->create($_POST)) {
				
				if ($this->db->save()!==false) {

					$this->db->data_cache();
					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		
		}else{
	
		$id= intval(I("get.id"));
		if(!$id){
			$this->error("缺少参数！");
		}

		$r=$this->db->where("id=$id")->find();
		$this->assign("edit",true);
		$this->assign("data",$r);
		$this->display("form");
		}
	}



	function  delete()
	{


		if(isset($_GET['id'])){
			$id = intval(I("get.id"));

			if ($this->db->delete($id)) {


				D("Xiaoqu")->where(array("shop_id"=>$id))->save(array("shop_id"=>""));
				D("Users")->where(array("shop_id"=>$id))->save(array("shop_id"=>""));


				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}



	}


}